require(['grid', 'topBar', 'layout'], function () {
    var lineId,
        $top = $('#top'),
        $grid = $('#grid');

    //顶部栏初始化
    function topBarInit() {
        $top.topBar({
            left: [
                {
                    text: '黑点名称',
                    name: 'blackName',
                    width: 165,
                    type: 'text'
                },
                {
                    text: '查询',
                    type: 'btn',
                    click: function () {
                        reloadBlackGridData();
                    }
                }
            ],
            right: []
        });
    }

    //grid初始化
    function gridInit() {
        $grid.grid({
            url: "/baseBlackArchivesInfo/searchBaseBlackArchivesInfoPage.htm",
            colModel: [
                {name: "rcrdId", label: 'rcrdId', key: true, hidden: true},
                {name: "blackName", label: '黑点名称'},
                {name: "blackNature", label: '黑点性质', dict: 'BLAK0000'},
                {
                    name: "dangerDegree", label: '危险度', align: 'center',
                    formatter: function (value) {
                        var html = '<span class="dangerous-setting">';
                        switch (value) {
                            case 1:
                                html += '<span class="active"></span><span></span><span></span><span></span><span></span>';
                                break;
                            case 2:
                                html += '<span class="active"></span><span class="active"></span><span></span><span></span><span></span>';
                                break;
                            case 3:
                                html += '<span class="active"></span><span class="active"></span><span class="active"></span><span></span><span></span>';
                                break;
                            case 4:
                                html += '<span class="active"></span><span class="active"></span><span class="active"></span><span class="active"></span><span></span>';
                                break;
                            case 5:
                                html += '<span class="active"></span><span class="active"></span><span class="active"></span><span class="active"></span><span class="active"></span>';
                                break;
                            default :
                                html += '<span></span><span></span><span></span><span></span><span></span>';
                                break;
                        }
                        html += '</span>';
                        return html;
                    }
                },
                {name: "highestSpdOfMotVhi", label: '最高限速'},
                {name: "areaRoad", label: '区域路段'}
            ],
            multiselect: true
        });
    }

    //查询grid
    window.reloadBlackGridData = function() {
        var nodes = commonFun.getLeftTreeCheckedNodes('LINE');

        if (nodes && nodes.length == 1) {
            lineId = nodes[0];
        }
        
        var postData = $top.getValues();
        postData.lineId = lineId;
        
        $grid.grid('setGridParam', {
            datatype: "JSON",
            page: 1,
            postData: postData
        }).trigger("reloadGrid");
    }

    //分配黑点
    window.distribute = function (fn) {
        var selectRowIds = $grid.getGridParam().selarrrow;

        $.ajaxExt({
            url: "/baseLineBlackRel/addBaseLineBlackRel.htm",
            data: {'lineId': lineId, 'rcrdIds': selectRowIds.toString()}
        }).done(function (data) {
            fn();
        });
    };

    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();

    //进页面后查询
    //reloadBlackGridData();
});